package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.SchoolAdapter;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.SearchSchoolTask;
import com.xgkj.diyiketang.util.EmptyDataUtil;
import com.xgkj.diyiketang.util.ToastUtil;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;

import java.util.ArrayList;

public class SearchSchoolActivity extends BaseActivity implements SchoolAdapter.SchoolListAdapterListener, PullToRefreshListView.OnRefreshListener, PullToRefreshListView.OnLoadDataListener {

    private Context mContext;
    private EditText searchEdit;
    private String keyword = "";
    private TextView title;
    private int type = 0;
    private String titleStr;
    private int resultcode;
    private LinearLayout mEmptyContentView;
    private TextView save;
    private PullToRefreshListView mListView;
    private int searchTaskId = -1;
    private int currentPage = 1;
    private int size = 100;
    private SchoolAdapter mAdapter;
    private LinearLayout mEmptyView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_school);
        mContext = this;
        initView();
        initData();
    }

    private void initData() {
        Intent intent = getIntent();
        if (intent != null) {
            type = intent.getIntExtra("type", 0);
            resultcode = intent.getIntExtra("resultcode", 111);
            titleStr = intent.getStringExtra("title");
        }
        title.setText(titleStr);
    }

    private void initView() {
        ((TextView) findViewById(R.id.back)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mListView = ((PullToRefreshListView) findViewById(R.id.search_school_pull_list));
        mListView.init(mContext, true);
        mAdapter = new SchoolAdapter(mContext);
        mAdapter.setAdapterListener(this);
        mListView.setAdapter(mAdapter);
        mListView.setOnRefreshListener(this);
        mListView.setOnLoadDataListener(this);
        mListView.doRefresh();

        mEmptyView = (LinearLayout) findViewById(R.id.content_error_empty);
        mEmptyView.setVisibility(View.GONE);
        mEmptyContentView = (LinearLayout) findViewById(R.id.empty_content_view);
        mEmptyContentView.setVisibility(View.GONE);
        save = ((TextView) findViewById(R.id.saveinfo));
        title = ((TextView) findViewById(R.id.title));
        searchEdit = (EditText) findViewById(R.id.search_edit);
//        searchEdit.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//
//            }
//        });
        searchEdit.setOnEditorActionListener(new EditText.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    keyword = searchEdit.getText().toString();
                    if (checkInput()) {
                        currentPage = 1;
                        getSchoolList();

                    }
                }
                return true;
            }
        });
    }

    private void getSchoolList() {
        if (searchTaskId != -1) {
            return;
        }
//        if (currentPage == 0) {
//            mListView.setNoNextPage();
//            return;
//        }

        SearchSchoolTask task = new SearchSchoolTask(keyword, size, currentPage, type);
        task.addListener(anchorListListener);
        searchTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    private TaskEventListener anchorListListener = new ShowToastTaskEventListener(this) {
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (data != null) {
                if (taskId == searchTaskId) {
                    searchTaskId = -1;
                    if (currentPage == 1) {
                        mAdapter.clearData();
                        mListView.onRefreshComplete();
                        mListView.enableLoadMore();
                    } else {
                        mListView.onLoadDataComplete(true);
                    }
                    ArrayList schoolList = new ArrayList<>();
                    schoolList = (ArrayList) data;
                    if (schoolList != null && schoolList.size() > 0) {
                        mAdapter.updateData(schoolList);
                    }

                    currentPage = result;
                    Log.e("schoollist", "result: " + result);
                    if (mAdapter != null && mAdapter.getCount() == 0) {
                        EmptyDataUtil.updateWenziEmptyContentView(mEmptyView, mEmptyContentView, mListView);
                    } else {
                        EmptyDataUtil.updateNoEmptyContentView(mEmptyView, mEmptyContentView, mListView);
                    }

                }
            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if (taskId == searchTaskId) {
                searchTaskId = -1;
                if (currentPage == 1) {
                    mListView.onRefreshComplete();
                } else {
                    mListView.onLoadDataComplete(false);
                }
            }
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }

    };


    private boolean checkInput() {
        if (keyword == null || (keyword.trim()).equals("")) {
            ToastUtil.showMessage(mContext, R.string.search_no_null);
            return false;
        }
        return true;
    }


    @Override
    public void onRefresh() {
        currentPage = 1;
        getSchoolList();
    }

    @Override
    public void onPullRefresh() {

    }

    @Override
    public void onLoadMore() {
        getSchoolList();
    }

    @Override
    public void onClick(String school) {
        Intent intent = new Intent(SearchSchoolActivity.this, EduExperienceActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("school", school);
        intent.putExtra("bundle", bundle);
        setResult(resultcode, intent);
        this.finish();
    }
}
